var GLOBAR = {};
(function(){
	/**
	 * 创建命名空间
	 */
	GLOBAR.namespace = function(str){
		var arr = str.split("."),o=GLOBAR;
		for(var i = (arr[0] ==" GLOBAR") ? 1 : 0, j = arr.length;i < j; i++){
			o[arr[i]] = o[arr[i]] || {};
			o = o[arr[i]];
		}
	};
	GLOBAR.namespace("base");
	GLOBAR.namespace("tab");
	GLOBAR.namespace("combo"); //存储下拉框的值
	
	/**
	 * 初始化dataGrid
	 * 工具栏默认有新增、删除、修改
	 */
	GLOBAR.base.dataGrid = function (obj){
		var toolbar = [];
		if(obj.barData){
			if(obj.barData.add){
				var addbar = {
						text: obj.barData.add.title || '新增',
						iconCls: obj.barData.add.iconCls || 'icon-add',
						handler: function(){
							var barObj={
									    bodyId:obj.bodyId,
									    tid:obj.tid,
									    url:obj.barData.add.url,
									 	title: obj.barData.add.title|| '新增'
									    };
							GLOBAR.base.OpenDialog(barObj);
						}
					};
				toolbar.push(addbar);
				toolbar.push('-');
			}
		}
		
		if(toolbar.length == 0){
			toolbar = null; //如果没有工具栏, 就让他消失
		}else{
			toolbar.pop(); //把最后一个 '-' 吐出来
		}
		
		
		/**
		 * 工具栏 barData
		 * 参数:barData.add,barData.add.url,barData.add.title
		 * 参数:barData.del,barData.del.url,barData.del.rid
		 */
		var param = {
			idField: "id", 
			nowrap: false,
			striped: true,
			border:false,
			fit: true,
			sortOrder: 'desc',
			pagination:true,
			pageList:[20,30,40,50,100,200],
			pageSize:20,
			rownumbers:true,
			singleSelect:true,
			toolbar:toolbar,
			frozenColumns:[[
      			{field:'ck',checkbox:true},
      			{title:'序号',field:'id',width:40,hidden:false}
      		]]
		};
		//参数合并
		$.extend(param, obj);
		$(param.tid).datagrid(param);
	};
	
	GLOBAR.base.OpenDialog=function(param){
		var dialogDivId=param.bodyId.substring(1,param.bodyId.length);
		$(param.bodyId).append("<div id='"+dialogDivId+"'></div>");
	}
	
})();